package com.yungu.swift.admin.route.marketing;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.admin.utils.ExcelUtil;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminUuidParam;
import com.yungu.swift.base.model.annotation.ResponseImgOverride;
import com.yungu.swift.common.model.dto.CommonArticleDto;
import com.yungu.swift.common.model.param.AdminArticlePageParam;
import com.yungu.swift.common.model.param.AdminArticleParam;
import com.yungu.swift.common.service.CommonArticleService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : cuixiuyin
 * @date : 2019/7/29
 */
@RestController
@RequestMapping("/admin/v1/marketing/article")
@Api(value = "【营销管理】", tags = "文章管理")
public class ArticleAdmin {

    @Reference
    private CommonArticleService commonArticleService;

    @ResponseImgOverride
    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "文章信息分页查询", httpMethod = "POST")
    public ResponseData<PageVo<CommonArticleDto>> queryPage(@RequestBody AdminArticlePageParam articlePageParam) {
        return commonArticleService.queryPage(articlePageParam);
    }

    @ResponseImgOverride
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "文章信息详情", httpMethod = "GET")
    public ResponseData<CommonArticleDto> detail(@RequestParam String uuid) {
        if (ParamUtil.isIllegal(uuid)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return commonArticleService.get(uuid);
    }

    @RequestMapping(value = "/visit/count", method = RequestMethod.GET)
    @ApiOperation(value = "访问统计计数", httpMethod = "GET")
    public ResponseData<Boolean> visitCount(@RequestParam String uuid) {
        if (ParamUtil.isIllegal(uuid)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return commonArticleService.visitCount(uuid);
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "文章信息新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminArticleParam articleParam) {
        return commonArticleService.saveOrUpdate(articleParam);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ApiOperation(value = "文章信息删除", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminUuidParam uuidParam) {
        if (ParamUtil.isIllegal(uuidParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return commonArticleService.del(uuidParam.getUuid(), CommonArticleDto.class);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "文章导出", httpMethod = "POST")
    public void export(@RequestBody AdminArticlePageParam articlePageParam, HttpServletResponse response) {
        String templatePath = "/template/article.xlsx";
        List<CommonArticleDto> list = commonArticleService.list(MapUtils.convertObjToMap(articlePageParam)).getData();
        ExcelUtil.export(response, list, CommonArticleDto.class, templatePath, ExcelUtil.getFileName("文章"), null, "yyy/MM/dd hh:mm:ss");
    }

}
